﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameEndUIControll : MonoBehaviour
{
    public int score;
    public List<int> historyScore;
    public int scoreNum;

    [SerializeField]
    private Text FinalScore;

    [SerializeField]
    private Button ReStart;

    [SerializeField]
    private GameObject HistoryScoreGenerator;

    [SerializeField]
    private GameObject OneLineScore;

    [SerializeField]
    List<GameObject> AllLineScores;

    // Start is called before the first frame update
    void Start()
    {
        FinalScore.text = score.ToString();
        DisplayHistoryScore();

        ReStart.onClick.AddListener(
                delegate
                {
                    GlobalEventManager.SendRestartRequest();
                }
            );
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //创建排行文本
    void DisplayHistoryScore()
    {
        for(int i = 0; i < scoreNum; i++)
        {
            GameObject scoreLine = Instantiate(OneLineScore, HistoryScoreGenerator.transform.position, Quaternion.identity, transform);
            AllLineScores.Add(scoreLine);
            Vector3 originPosition = HistoryScoreGenerator.transform.position;
            RectTransform rect = OneLineScore.transform as RectTransform;
            Vector3 newPosition = originPosition - new Vector3(0, rect.sizeDelta.y * transform.lossyScale.y);
            HistoryScoreGenerator.transform.position = newPosition;

            if(i < historyScore.Count)
            {

                Transform orderText = scoreLine.transform.Find("Order/OrderText");
                if(orderText != null)
                {
                    Text text = orderText.GetComponent<Text>();
                    text.text = (i + 1).ToString();
                }

                Transform scoreText = scoreLine.transform.Find("Score/ScoreText");
                if(scoreText != null)
                {
                    Text text = scoreText.GetComponent<Text>();
                    text.text = historyScore[i].ToString();
                }
            }
            else
            {

                Transform orderText = scoreLine.transform.Find("Order/OrderText");
                if (orderText != null)
                {
                    Text text = orderText.GetComponent<Text>();
                    text.text = (i + 1).ToString();
                }

                Transform scoreText = scoreLine.transform.Find("Score/ScoreText");
                if (scoreText != null)
                {
                    Text text = scoreText.GetComponent<Text>();
                    text.text = "-----";
                }
            }
        }

        //判断排行榜中是否存在玩家分数，如果存在，激活newicon
        for(int i = 0; i < historyScore.Count; i++)
        {
            if(historyScore[i] == score)
            {
                Transform newIcon = AllLineScores[i].transform.Find("NewIcon");
                newIcon.gameObject.SetActive(true);
                break;
            }
        }
    }
}
